package org.ace.view
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import org.ace.communicate.MessageReceived;
	import org.ace.model.MyModel;
	import org.ace.protocol.MapProtocol;
	import org.ace.protocol.Protocol;
	
	public class IMHandler extends Sprite
	{
		private static var im:IMHandler;
		private var load:Loader;
		private var imView:MovieClip;
		private var uiView:MovieClip;
		private var state:int=0;
		public function IMHandler()
		{
			super();
			load=new Loader();
			load.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoad);
			load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);
			Loading.getInstance().addLoading();
			Loading.getInstance().setBody("正在加载UI资源");
			load.load(new URLRequest("res/message.swf"));
			
		}
		public static function getIm():IMHandler{
			if(im==null){
				im=new IMHandler();
			}
			return im;
		}
		
		public function loading(e:ProgressEvent):void{
			Loading.getInstance().setIng((e.bytesLoaded/e.bytesTotal*100).toString());
		}
		
		public function onLoad(e:Event):void{
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoad);
			var myclass:Class =load.contentLoaderInfo.applicationDomain.getDefinition("org.ace.messagePanel") as Class;
			imView=MovieClip(new myclass);
			//imView.input.login.addEventListener(MouseEvent.CLICK,onClick);
			imView.x=0;
			imView.y=560;
			imView.chatPanel.sendBtn.addEventListener(MouseEvent.CLICK,onClick);
			addChild(imView);
			load=new Loader();
			load.contentLoaderInfo.addEventListener(Event.COMPLETE,uiOnLoad);
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.load(new URLRequest("res/ui.swf"));
		//	IMHandler.getIm().visible=false;
			
		}
		public function uiOnLoad(e:Event):void{
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.contentLoaderInfo.removeEventListener(Event.COMPLETE,uiOnLoad);
			var myclass:Class =load.contentLoaderInfo.applicationDomain.getDefinition("org.ace.uis") as Class;
			uiView=MovieClip(new myclass);
			//imView.input.login.addEventListener(MouseEvent.CLICK,onClick);
			uiView.x=960;
			uiView.y=565-uiView.height;
			uiView.addEventListener("uiclick",uiClick);
			addChild(uiView);
			Loading.getInstance().removeLoading();
			MyGameDemo.mView.addChild(IMHandler.getIm());
			
		}
		private function uiClick(e:MyEvent):void{
			switch(e.EventType){
				case TypeName.RoleEvent:
					mapMessage("点击了角色按钮");
					break;
				case TypeName.BagEvent:
					mapMessage("点击了背包按钮");
					break;
				case TypeName.FriendEvent:
					mapMessage("点击了好友按钮");
					break;
				case TypeName.FactionEvent:
					mapMessage("点击了帮派按钮");
					break;
				case TypeName.SysEvent:
					mapMessage("点击了系统按钮");
					break;
				case TypeName.MissionEvent:
					mapMessage("点击了任务按钮");
					break;
				case TypeName.PetEvent:
					mapMessage("点击了宠物按钮");
					break;
				case TypeName.SkillEvent:
					mapMessage("点击了技能按钮");
					break;
				case TypeName.TeamEvent:
					mapMessage("点击了组队按钮");
					break;
			}
			
		}
		public function onClick(e:MouseEvent):void{
			if(state==0){
				if(imView.chatPanel.textInput.text!=""){
					MessageReceived.getMessageReceived().write(Protocol.TYPE_MAP,MyModel.mapId,MapProtocol.send_message_creq,imView.chatPanel.textInput.text);
					imView.chatPanel.textInput.text="";
				}
				
			}
		}
		public function mapMessage(m:String):void{
			imView.msgPanel.contentText.appendText(m+"\n");
			if(imView.msgPanel.contentText.height<100){
				imView.msgPanel.contentText.height+=12;
				imView.msgPanel.contentText.y-=12;}
			imView.msgPanel.contentText.scrollV=imView.msgPanel.contentText.maxScrollV;
			if(imView.msgPanel.contentText.numLines>50){
				var hh:int=imView.msgPanel.contentText.getLineOffset(1);
				imView.msgPanel.contentText.replaceText(0,hh,"");
			}

		}
	}
}